package com.sakila.web;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletContext;

import javax.sql.DataSource;

import com.sakila.util.DBConnection;

@WebListener
public class Init implements ServletContextListener {


    
    public void contextDestroyed(ServletContextEvent sce) {
    }
    
    private void contextInitialized2(ServletContext servletContext)
    throws Exception {
       InitialContext enc = new InitialContext();
       Context compContext = (Context) enc.lookup("java:comp/env");
       DataSource dataSource = (DataSource) compContext.lookup("sakiladatasource");
       DBConnection.setDataSource(dataSource);
//       NewsFeedServlet.setDataSource(dataSource);

    }

    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();
        try {
           contextInitialized2(servletContext);
        }
        catch (Exception e)
        {
           
           throw new RuntimeException(e);
        }
        
    }
}

